package cn.com.dashihui.web.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Seller;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.service.EcardOrderService;
import cn.com.dashihui.web.service.StoreService;

@RequiresAuthentication
public class EcardOrderController extends BaseController {
	//使用Duang.duang进行封装，使普通类具有事务的功能
	private EcardOrderService service = Duang.duang(EcardOrderService.class);
	private StoreService storeService = new StoreService();
	/**
	 * 订单列表页面
	 */
	public void index(){
		List<Record> list=storeService.findAllSeller();
    	setAttr("list", list);
		render("index.jsp");
	}
	/**
	 * 获得订单列表数据
	 */
	public void page(){
		System.out.println("获得订单列表数据");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		//公司ID
		String sellerId=getPara("sellerId");
		//分公司ID
		String storeId=getPara("storeId");
		Page<Record> findByPage = service.findByPage(pageNum,pageSize,orderNum,beginDate,endDate,state,sellerId,tel,storeId);
		renderResult(0,findByPage);
	}
	/**
	 * 获得特定用户的充值订单列表数据
	 */
	public void recharge(){
		System.out.println("获得订单列表数据");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		//公司ID
		String sellerId=getPara("sellerId");
		//分公司ID
		String storeId=getPara("storeId");
		//买家ID
		int userId = getParaToInt("userId",0);
		System.out.println("userIduserIduserIduserId"+userId);
		Page<Record> findByPage = service.findByPageRecharge(pageNum,pageSize,orderNum,beginDate,endDate,state,sellerId,tel,storeId,userId);
		renderResult(0,findByPage);
	}
	/**
	 * 获得特定用户的提现订单列表数据
	 */
	public void extract(){
		System.out.println("获得订单列表数据");
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		//公司ID
		String sellerId=getPara("sellerId");
		//分公司ID
		String storeId=getPara("storeId");
		//买家ID
		int userId = getParaToInt("userId",0);
		System.out.println("userIduserIduserIduserId"+userId);
		Page<Record> findByPage = service.findByPageExtract(pageNum,pageSize,orderNum,beginDate,endDate,state,sellerId,tel,storeId,userId);
		renderResult(0,findByPage);
	}
    /**
     * 订单统计查询所需积分总和现金价格总和
     */
	public void findTotal(){
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		int userid = getParaToInt("userId",0);
		//这里的storeid已经是主键id
		String storeid = getPara("storeid");
		int seid = 0;
		if (storeid!=null) {
			seid = Integer.parseInt(storeid);
		}
		
		System.out.println(seid);
		String tel=getPara("tel");
		String orderNum = getPara("orderNum");
		int state = getParaToInt("state",0);
		renderSuccess(service.findTotal(beginDate, endDate,storeid,tel,orderNum,state,seid,userid));
	}
	/**
	* 统计总提现金额,驳回金额，通过金额
	*/
	public void findTotalMoney(){
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		int userid = getParaToInt("userId",0);
		//这里的storeid已经是主键id
		String storeid = getPara("storeid");
		int seid = 0;
		if (storeid!=null) {
			seid = Integer.parseInt(storeid);
		}
		
		System.out.println(seid);
		String tel=getPara("tel");
		String name=getPara("name");
		String orderNum = getPara("orderNum");
		int state = getParaToInt("state",0);
		renderSuccess(service.findTotalMoney(beginDate, endDate,storeid,tel,name,orderNum,state,seid,userid));
	}

	/**
	 * 获得订单列表数据
	 */
	public void goodsOrder(){
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		//订单编号
		String orderNum = getPara("orderNum");
		//下单时间范围
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		//订单状态
		int state = getParaToInt("state",0);
		//买家电话
		String tel = getPara("tel");
		int seid = 0;
		int stid = 0;
		//公司ID
		String sellerId=getPara("sellerId");
		if (StrKit.notBlank(sellerId)) {
			Seller seller = storeService.findSeller(sellerId);
			seid = seller.getInt("id");
		}
		//分公司ID
		String storeId=getPara("storeId");
		if (StrKit.notBlank(storeId)) {
			Store store = storeService.findByStoreid(storeId);
	 		stid = store.getInt("id");
		}
		//商品Id
		int id=getParaToInt("id",0);
		renderResult(0,service.goodsOrder(pageNum,pageSize,orderNum,beginDate,endDate,state,seid,tel,stid,id));
	}

}
